﻿using System.Linq;
using System.IO;
using Inkubator.Wizard.Model.Xml;
using Inkubator.Wizard.Model.Model;
using Inkubator.Wizard.WebWizard.WizardControl;
using System.Web.SessionState;

namespace Inkubator.Wizard.WizardGenerator.Generator
{
    /// <summary>
    /// This class received a XML File and generate the Wizard Model to
    /// be able to create the Wizard Control.
    /// </summary>
    public class WizardGenerator
    {
        private WizardModel wizardModel;

        public WizardGenerator(Stream stream)
        {
            XmlOperations xml = new XmlOperations();
            wizardModel = xml.deserialize(stream);
        }

        public WizardGenerator(string xmlPath, string xsdPath)
        {
            XmlOperations xml = new XmlOperations();
            wizardModel = xml.deserialize(xmlPath, xsdPath);
        }

        /// <summary>
        /// This method create the Wizard Control instanciating each step.
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public WizardWeb obtainWizardControl( HttpSessionState session )
        {
            WizardWeb wizardControl = new WizardWeb();
            wizardControl.setActiveStep(session);
            for (int i = 0; i < wizardModel.steps.Count; i++)
            {
                //Create WizardStep and Obtain Step from WizardModel
                Step step = wizardModel.steps.ElementAt(i);

                //Add components to Step
                StepDesigner stepDesigner = new StepDesigner();
                StepWizard stepW = stepDesigner.addStepControls(step);
                stepW.ID = step.ID;

                //Set action properties
                if (step.doActions != null)
                {
                    stepW.setAction(step.doActions.assemblyName, step.doActions.method, step.doActions.impInterface);
                }                

                //Add Step to Wizard Control
                wizardControl.addStep(stepW);
            }

            if (this.wizardModel.stepProgress != null)
            {
                wizardControl.setStepProgress(this.wizardModel.stepProgress);
            }
            if (this.wizardModel.postAction != null)
            {
                wizardControl.setPostAction(this.wizardModel.postAction.assemblyName, this.wizardModel.postAction.method,
                    this.wizardModel.postAction.impInterface);
            }
            wizardControl.setWizardTitle(this.wizardModel.title);

            wizardControl.Bind();

            return wizardControl;
        }

        /// <summary>
        /// Return the WizardModel object that represent the XML File that was readed.
        /// </summary>
        /// <returns></returns>
        public WizardModel getWizardModel()
        {
            return this.wizardModel;
        }

        /// <summary>
        /// Execute an Action before the Wizard start.
        /// </summary>
        public void executePreAction()
        {
            if (this.wizardModel.preActionn != null)
            {
                object[] array = new object[] { };

                ActionWizard preAction = new ActionWizard(this.wizardModel.preActionn.assemblyName,
                    this.wizardModel.preActionn.method, this.wizardModel.preActionn.impInterface);

                preAction.execute(array);
            }
        }

    }
}
